package com.chiaching.chaoxingsecondkill.controller;

import com.chiaching.chaoxingsecondkill.domain.UserInfo;
import com.chiaching.chaoxingsecondkill.hander.SecondKillHandler;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Objects;

/**
 * @author jiajing.he
 * @date 2021/10/9 上午9:55
 */
@Controller
@RequestMapping("index")
public class IndexController {

    @GetMapping("")
    public String index(ModelMap modelMap){
        List<UserInfo> userInfos = SecondKillHandler.USER_CONFIG.getList();
        modelMap.addAttribute("userInfos", userInfos);
        return "index";
    }

    @GetMapping("operation")
    public String operation(String userName,boolean isStart,ModelMap modelMap){
        List<UserInfo> userInfos = SecondKillHandler.USER_CONFIG.getList();
        modelMap.addAttribute("userInfos", userInfos);
        if (StringUtils.isNotBlank(userName)){
            UserInfo userInfo = SecondKillHandler.USER_CONFIG.getList().stream().filter(e -> e.getName().equals(userName)).findFirst().orElse(null);
            if (Objects.nonNull(userInfo)){
                userInfo.setIsStart(isStart);
            }
        }
        return "index";
    }

}
